package com.yuexin.project.store.stock.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.DateUtils;
import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.store.stock.domain.Stock;
import com.yuexin.project.store.stock.service.IStockService;

/**
 * 物品管理 信息操作处理
 * 
 * @author yuexin
 * @date 2018-11-30
 */
@Controller
@RequestMapping("/store/stock")
public class StockController extends BaseController
{
    private String prefix = "store/stock";
	
	@Autowired
	private IStockService stockService;
	
	@RequiresPermissions("store:stock:view")
	@GetMapping()
	public String stock()
	{
	    return prefix + "/stock";
	}
	
	/**
	 * 查询物品管理列表
	 */
	@RequiresPermissions("store:stock:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Stock stock)
	{
		startPage();
		stock.setCompanyId(ShiroUtils.getCompanyId());
        List<Stock> list = stockService.selectStockList(stock);
		return getDataTable(list);
	}
	
	/**
	 * 修改物品管理
	 */
	@GetMapping("/edit/{goodsId}")
	public String edit(@PathVariable("goodsId") Integer goodsId, ModelMap mmap)
	{
		Stock stock = stockService.selectStockById(goodsId);
		mmap.put("stock", stock);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存物品管理
	 */
	@RequiresPermissions("store:stock:edit")
	@Log(title = "物品管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Stock stock)
	{		
		stock.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(stockService.updateStock(stock));
	}
	
	/**
	 * 导出 
	 */
    @RequiresPermissions("store:stock:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Stock stock)
    {
		stock.setCompanyId(ShiroUtils.getCompanyId());
        List<Stock> list = stockService.selectStockList(stock);
        for(Stock i : list){
        	Date updateTime = i.getUpdateTime();
        	if(updateTime != null) {
        		String updateTimeExcel = DateUtils.dateTime(updateTime);
            	i.setUpdateTimeExcel(updateTimeExcel);
        	}
        }
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        return util.exportExcel(list, "Stock");
    }
	
}
